package com.example.administrator.spritelgs;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.example.administrator.spritelgs.adapter.SubViewAdapter;
import com.example.administrator.spritelgs.util.AnimUtil;
import com.example.administrator.spritelgs.view.BeibaoView;
import com.example.administrator.spritelgs.view.ShopView;
import com.example.administrator.spritelgs.view.ShouYeView;

import java.util.ArrayList;
import java.util.List;

public class gameActivity extends Activity implements View.OnTouchListener{
    private Button btnMsg;
    private ProgressBar pb_timer_l;
    private ProgressBar pb_timer_r;
    private LinearLayout SubBtns;
    private FrameLayout iv_flod;

    private FrameLayout fl_beibao;
    private FrameLayout fl_rongyu;
    private FrameLayout fl_liuyan;
    private FrameLayout fl_renwu;
    private FrameLayout fl_chuzhan;
    private FrameLayout fl_hecheng;


    private ImageView iv_shouye;
    private ImageView iv_level;
    private ImageView iv_raid;
    private ImageView iv_shop;
    private ImageView iv_friends;
    private ImageView iv_setting;

    private RelativeLayout rl_root;
    private ViewPager vp_content;
    private List<View> subViewList;

    private ShouYeView shouYeView;
    private ShopView shopView;

    private BeibaoView beibaoView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        initUI();
        updateTimer();
        initSubViewList();
    }

    private void updateTimer(){
        new Thread(){
            @Override
            public void run() {
                while(true){
                    int p = pb_timer_l.getProgress();
                    if(p==100) p=-1;
                    p+=1;
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    pb_timer_l.setProgress(p + 1);
                }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                while(true){
                    int p = pb_timer_r.getProgress();
                    if(p==100) p=-1;
                    p+=1;
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    pb_timer_r.setProgress(p + 1);
                }
            }
        }.start();
    }

    private void initSubViewList(){
        subViewList = new ArrayList<View>();
        shouYeView = new ShouYeView(this);
        shopView = new ShopView(this);
        beibaoView = new BeibaoView(this);

        subViewList.add(shouYeView.getView());
        subViewList.add(shopView.getView());

        SubViewAdapter adapter = new SubViewAdapter(getApplicationContext(), subViewList);
        vp_content.setAdapter(adapter);
    }

    private void initUI()
    {
        btnMsg = (Button) findViewById(R.id.btnMsg);
        AnimUtil.addOnTouchListener(btnMsg, 0f, 0f, null);

        pb_timer_l = (ProgressBar) findViewById(R.id.pb_timer_l);
        pb_timer_r = (ProgressBar) findViewById(R.id.pb_timer_r);

        vp_content = (ViewPager) findViewById(R.id.vp_content);
        //禁止滑动
        vp_content.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });

        SubBtns = (LinearLayout)findViewById(R.id.SubBtns);

        iv_flod = (FrameLayout)findViewById(R.id.iv_fold);
        AnimUtil.addOnTouchListener(iv_flod, 0.5f, 1f, animListener);

        fl_beibao = (FrameLayout)findViewById(R.id.fl_beibao);
        AnimUtil.addOnTouchListener(fl_beibao, 0.5f, 1f, new AnimUtil.AnimListener() {
            @Override
            public void down(View v) {

            }

            @Override
            public void move(View v) {

            }

            @Override
            public void up(View v) {
                beibaoView.show();
            }
        });
        fl_rongyu = (FrameLayout)findViewById(R.id.fl_rongyu);
        AnimUtil.addOnTouchListener(fl_rongyu, 0.5f, 1f, null);
        fl_liuyan = (FrameLayout)findViewById(R.id.fl_liuyan);
        AnimUtil.addOnTouchListener(fl_liuyan, 0.5f, 1f, null);
        fl_renwu = (FrameLayout)findViewById(R.id.fl_renwu);
        AnimUtil.addOnTouchListener(fl_renwu, 0.5f, 1f, null);
        fl_chuzhan = (FrameLayout)findViewById(R.id.fl_chuzhan);
        AnimUtil.addOnTouchListener(fl_chuzhan, 0.5f, 1f, null);
        fl_hecheng = (FrameLayout)findViewById(R.id.fl_hecheng);
        AnimUtil.addOnTouchListener(fl_hecheng, 0.5f, 1f, null);

        iv_shouye = (ImageView)findViewById(R.id.iv_shouye);
        AnimUtil.addOnTouchListener(iv_shouye, 0.5f, 1f, animListener);
        iv_level = (ImageView)findViewById(R.id.iv_level);
        AnimUtil.addOnTouchListener(iv_level, 0.5f, 1f, animListener);
        iv_raid = (ImageView) findViewById(R.id.iv_raid);
        AnimUtil.addOnTouchListener(iv_raid, 0.5f, 1f, animListener);
        iv_shop = (ImageView)findViewById(R.id.iv_shop);
        AnimUtil.addOnTouchListener(iv_shop, 0.5f, 1f, animListener);
        iv_friends = (ImageView)findViewById(R.id.iv_friends);
        AnimUtil.addOnTouchListener(iv_friends, 0.5f, 1f, animListener);
        iv_setting = (ImageView)findViewById(R.id.iv_setting);
        AnimUtil.addOnTouchListener(iv_setting, 0.5f, 1f, animListener);

        rl_root = (RelativeLayout)findViewById(R.id.rl_root);
        vp_content = (ViewPager)findViewById(R.id.vp_content);

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        //获取当前事件的状态
        int action = event.getAction();
        switch (action)
        {
            case MotionEvent.ACTION_DOWN:
                Log.i("gameActivity", "down");
                ScaleAnimation scale = new ScaleAnimation(1.f, 0.75f, 1.f, 0.75f,
                        Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f);
                scale.setDuration(300);
                scale.setFillAfter(true);//保持动画执行完的状态
                v.startAnimation(scale);
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i("gameActivity", "move");
                break;
            case MotionEvent.ACTION_UP:
                Log.i("gameActivity", "up");
                scale = new ScaleAnimation(0.75f, 1f, 0.75f, 1f,
                        Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f);
                scale.setDuration(300);
                scale.setFillAfter(true);//保持动画执行完的状态
                v.startAnimation(scale);
                break;
        }
        return false;
    }

    private AnimUtil.AnimListener animListener = new AnimUtil.AnimListener() {
        boolean isDown;

        @Override
        public void down(View v) {

        }

        @Override
        public void move(View v) {

        }

        @Override
        public void up(View v) {
            switch (v.getId()){
                case R.id.iv_shop:
                    Log.i("gameActivity", "shop");
                    if(vp_content.getCurrentItem() == 1)
                        break;
                    vp_content.setCurrentItem(1);
                    rl_root.setBackgroundResource(R.mipmap.bg_shop);
                    shopView.show();
                    break;
                case R.id.iv_shouye:
                    Log.i("gameActivity", "shouye");
                    shopView.hide(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            vp_content.setCurrentItem(0);
                            rl_root.setBackgroundResource(R.mipmap.background);
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });
                    break;
                case R.id.iv_fold:
                    int subHeight = SubBtns.getHeight() - 52;
                    if (isDown){
                        AnimUtil.addObjectAnimation(v, new String[]{AnimUtil.PROPERTY_TRANSLATIONY, AnimUtil.PROPERTY_ROTATIONX,
                                AnimUtil.PROPERTY_ROTATIONY}, 0f);
                        AnimUtil.addObjectAnimation(v, new String[]{AnimUtil.PROPERTY_ROTATIONX,
                                AnimUtil.PROPERTY_ROTATIONY}, 180f, 360f);
                        AnimUtil.addObjectAnimation(SubBtns, new String[]{AnimUtil.PROPERTY_TRANSLATIONY}, 0f);
                    }else {
                        AnimUtil.addObjectAnimation(v, new String[]{AnimUtil.PROPERTY_TRANSLATIONY, AnimUtil.PROPERTY_ROTATIONX,
                                AnimUtil.PROPERTY_ROTATIONY}, subHeight);
                        AnimUtil.addObjectAnimation(v, new String[]{AnimUtil.PROPERTY_ROTATIONX,
                                AnimUtil.PROPERTY_ROTATIONY}, 0f, 180f);
                        AnimUtil.addObjectAnimation(SubBtns, new String[]{AnimUtil.PROPERTY_TRANSLATIONY}, subHeight);
                    }
                    isDown = !isDown;
                    break;
            }
        }
    };
}
